var url = "http://127.0.0.1:8083/";

function toPage(pageName) {
    $.ajax({
        url:url + "user/"+pageName,
        type:"GET",
        async:false,
        success:function (data) {
            if (data["code"] == 200 || data["code"] == 204) {
                window.location.href=url + "user" + data["data"]+".html";
            }
        }
    })
}

//登录
function sub() {
    var username = $("#TANGRAM__PSP_4__userName").val();
    var password = $("#TANGRAM__PSP_4__password").val();
    var user = {
        userName:username,
        password:password
    };

    if (username == "" || username == null){
        $("#msg").css("display","block");
        $("#msg").html("请输入用户名/手机");
    }else if (password == "" || password == null) {
        $("#msg").css("display","block");
        $("#msg").html("请输入密码");
    }else {
        $.ajax({
            type:"POST",
            url:url + "user/login",
            contentType:"application/json;charset=UTF-8",
            dataType:"json",
            async:false,
            data:JSON.stringify(user),
            success:function (data) {
                if(data["code"] != 200){
                    $("#msg").css("display","block");
                    $("#msg").html(data["message"]);
                }else {
                    $("#msg").css("display","none");
                    console.log(data);
                    toPage("index");
                }
            }
        });
    }
}

var code_phone;
var countdown=60;

//发送验证码
function getcode(){
    var obj = $("#getCode");
    var phone = $("#TANGRAM__PSP_4__Phone").val();
    if (phone == null || phone == "") {
        $("#msg").css("display", "block");
        $("#msg").css("color", "red");
        $("#msg").html("请输入手机号");
    }else {
        $.ajax({
            type: "POST",
            url: url + "user/getCode",
            data: {phone:phone},
            error: function () {
                alert("验证码获取失败")
            },
            success: function (data) {
                if (data == false) {
                    $("#msg").css("display", "block");
                    $("#msg").css("color", "red");
                    $("#msg").html("请输入有效手机号");
                }else {
                    code_phone = data["data"];
                    console.log(code_phone);
                }
            }
        });
        settime(obj);
    }
}

// 发送验证码倒计时
function settime(obj) {
    if (countdown == 0) {
        obj.removeattr("disabled");
        obj.val("点击获取验证码");
        countdown = 60;

        return;
    } else {
        obj.attr('disabled',true);
        obj.val("重新发送(" + countdown + ")");
        countdown--;
    }
    setTimeout(function() {
            settime(obj)
        }, 1000)
}

//验证手机
function checkPhone() {
    var phone = $("#TANGRAM__PSP_4__Phone").val();
    var em = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (phone == null || phone == "") {
        $("#msg").css("display", "block");
        $("#msg").css("color", "red");
        $("#msg").html("请输入手机号");
    }else {
        if (!em.test(phone)) {
            $("#msg").css("display", "block");
            $("#msg").css("color", "red");
            $("#msg").html("手机号码格式不正确");
            $("#TANGRAM__PSP_4__submit").attr("disabled",true);
        } else {
            $("#msg").css("dispalay", "none");
            $("#msg").html("");
            $("#TANGRAM__PSP_4__submit").attr("disabled",false);
        }
    }
}

//验证用户名
function checkName() {
    var username = $("#TANGRAM__PSP_4__Name").val();
    if (username == null || username == "") {
        $("#msg").css("display", "block");
        $("#msg").css("color", "red");
        $("#msg").html("请输入用户名");
        return;
    }
    $.ajax({
        type: "POST",
        url: url + "user/checkName",
        data: {username:username},
        async:false,
        error: function () {
            alert("请求失败");
        },
        success: function (data) {
            if (data["data"] == true) {
                $("#msg").css("display", "block");
                $("#msg").css("color", "red");
                $("#msg").html("用户名已被占用");
                $("#TANGRAM__PSP_4__submit").attr("disabled",true);
            } else {
                $("#msg").css("display", "none");
                $("#TANGRAM__PSP_4__submit").attr("disabled",false);
            }
        }
    })
}

//密码验证
function checkPass() {
    var patrn=/^(\w){6,20}$/;
    var pass = $("#TANGRAM__PSP_4__password").val();
    if (pass == null || pass == "") {
        $("#msg").css("display", "block");
        $("#msg").css("color", "red");
        $("#msg").html("请输入密码");
        $("#TANGRAM__PSP_4__submit").attr("disabled",true);
        return;
    }
    if (!patrn.test(pass)) {
        $("#msg").css("display","block");
        $("#msg").css("color","red");
        $("#msg").html("只能输入6-20个字母、数字、下划线");
        $("#TANGRAM__PSP_4__submit").attr("disabled",true);
    }else {
        $("#msg").css("display","none");
        $("#TANGRAM__PSP_4__submit").attr("disabled",false);
    }
}

//提交验证
function submit_check(obj) {
    var phone = $("#TANGRAM__PSP_4__Phone").val();
    var name = $("#TANGRAM__PSP_4__Name").val();
    var pass = $("#TANGRAM__PSP_4__password").val();
    var code = $("#pass-phoenix-list-login").val();
    if (phone == null || phone == "") {
        $("#msg").css("display", "block");
        $("#msg").css("color", "red");
        $("#msg").html("请输入手机号");
        obj.attr("disabled",true);
    }else if (name == null || name == "") {
        $("#msg").css("display", "block");
        $("#msg").css("color", "red");
        $("#msg").html("请输入用户名");
        obj.attr("disabled",true);
    }else if (pass == null || pass == "") {
        $("#msg").css("display", "block");
        $("#msg").css("color", "red");
        $("#msg").html("请输入密码");
        obj.attr("disabled",true);
    }else if (code == null || code == "") {
        $("#msg").css("display", "block");
        $("#msg").css("color", "red");
        $("#msg").html("请输入验证码");
        obj.attr("disabled",true);
    }else {
        var user = {};
        user["userName"] = name;
        user["password"] = pass;
        user["phone"] = phone;
        if (code_phone == code){
            console.log(user);
            $.ajax({
                url:url + "user/register",
                type:"POST",
                contentType:"application/json;charset=UTF-8",
                dataType:"json",
                data:JSON.stringify(user),
                success:function (data) {
                    if (data["code"] == 200) {
                        toPage(data["data"]);
                    }else {
                        $("#msg").css("display", "block");
                        $("#msg").css("color", "red");
                        $("#msg").html(data["message"]);
                    }
                }
            });
        }else {
            $("#msg").css("display", "block");
            $("#msg").css("color", "red");
            $("#msg").html("验证码输入错误");
            obj.attr("disabled",true);
        }

    }
}